package cn.ivicar.iov.handler;

import cn.ivicar.iov.entity.Result;
import cn.ivicar.iov.exception.IovException;
import cn.ivicar.iov.util.ResultUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 异常处理
 */
@ControllerAdvice
public class ExceptionHandle {

    private final static Logger logger = LoggerFactory.getLogger(ExceptionHandle.class);

    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Result handle(Exception e){
        if(e instanceof IovException){
            IovException userException = (IovException) e;
            return ResultUtil.error(userException.getMessage());
        }else {
            logger.error("[系统错误]",e);
            return ResultUtil.error("未知错误");
        }
    }
}
